// pages/onlineshop/onlineshop.js
const { ticketadd } = require("../request/api");
const api = require("../request/api")
const request = require("../request/request")
const util = require("../utils/util")
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 254,
    uid: 9,
    goods_id: 0,
    goods_detail: '',
    category_list: [],
    good_list: [],
    uid: 0,
    page: 1,
    page_size: 15,
    current_index: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      uid: util.getAppValue(App.globalData.uid)
    })

    this.get_category_list()

    // this.get_goods_detail()
  },

  get_goods_detail() {
    let params = {
      id: this.data.id,
    }
    request.post(api.goods_details, params).then(res => {
      this.setData({
        goods_detail: res
      })
    })
  },

  go_search() {
    wx.navigateTo({
      url: '/pages/good/search',
    })
  },

  go_register() {
    wx.navigateTo({
      url: "/pages/shop/shop_register",
    })
  },

  go_vipwelfare() {
    wx.navigateTo({
      url: "/pages/vipwelfare/vipwelfare",
    })
  },

  cart_add(){
    util.showLoading("正在添加")
    let params = {
      // goods_num: this.data.cart_num,
      goods_id: this.data.goods_id,
      uid: this.data.uid
    }
    request.post(api.cart_add, params).then(res=>{
      this.setData({
        if_show_box:false
      })
      util.showToast("已加入购物车")
    })
  },
  go_detail_from_component(e) {
    let id = e.detail.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/good/detail?id=' + id,
    })
  },
  go_detail(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/good/detail?id=' + id + "&entrance=hot_good_around",
    })
  },
  get_category_list(index=0) {
    let params = {
      uid: this.data.uid,
      page: 1,
      pageSize: 10,
    }
    request.post(api.category_list, params).then(res=>{
      // res.reverse()
      let category_all = {
        id: 0,
        visibility: 1,
        name: "全部推荐",
      }
      let category_list = []
      res.unshift(category_all)
      this.setData({
        category_list: res
      })
      let type = res[index].id
      this.get_good_list(type)
    })
  },

  get_good_list(type) {
    let params = {
      uid: this.data.uid,
      page: this.data.page,
      pageSize: this.data.page_size,
      type,
    }
    request.post(api.goods_list, params).then(res=>{
      this.setData({
        good_list: res
      })
    })
  },

  choosing_category(e) {
    let current_index = e.currentTarget.dataset.index
    this.setData({
      current_index
    })
    let type = e.currentTarget.dataset.cate_id
    this.get_good_list(type)
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /**
   * 广告图点击事件处理函数
   */
  onAdTap: function (event) {
    // 当广告图被点击时执行的代码
    console.log('Advertisement is tapped!');
  }

})
